﻿using AutoMapper;
using B.S.Filtration.Domain.到货检验;
using B.S.Filtration.ErrorCode;
using B.S.Filtration.Write.Api.Application.Command.到货检验.QualityLnspectionCommnad;
using MediatR;
using SqlSugar;

namespace B.S.Filtration.Write.Api.Application.CommandHander.到货检验.QualityLnspectionHandler
{
    /// AddQualityLnspectionHandler：这是一个处理 AddQualityLnspectionCommnad 请求的类，继承自 IRequestHandler 接口。
    public class AddQualityLnspectionHandler : IRequestHandler<AddQualityLnspectionCommnad, APIResult<object>>
    {
        ///ISqlSugarClient ：这是一个 SQLSugar 的数据库客户端接口，用于执行数据库操作。
        private readonly ISqlSugarClient db;
        //IMapper：这是 AutoMapper 库 的核心接口，用于对象映射。
        private readonly IMapper mapper;
        //构造函数：AddQualityLnspectionHandler，接收两个参数 db 和 mapper。
        public AddQualityLnspectionHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }
        public async Task<APIResult<object>> Handle(AddQualityLnspectionCommnad request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var model = mapper.Map<QualityLnspection>(request);
            string yeartime = DateTime.Now.ToString("yyyyMMddHHmmss");
            string ProductCodess1 = "ZJGD" + yeartime;
            model.qualityWorkOrderNumber = ProductCodess1;
            model.IsDel = false;

            result.data = await db.Insertable(model).ExecuteCommandAsync();
            if (result.data != null)
            {
                result.code = APIResultCode.success;
                result.msg = "添加成功";
            }
            else
            {
                result.code = APIResultCode.failed;
                result.msg = "添加失败";
            }
            return result;
        }
    }
}
